package genericdemo;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-06-03
 * Time: 15:18
 */
// 泛型
class Alg<T extends Comparable<T>> {
//    public T[] array = (T[]) new Object[10];
//    public T[] array;
//    public genericdemo.Alg(){
//    }
//    public genericdemo.Alg(Class<T> clas,int capacity){
//        array = (T[]) Array.newInstance(clas,capacity);
//    }
    public T findMaxVal(T[] array){
        for (int i = 0; i < array.length-1; i++) {
            if(array[i].compareTo(array[i+1]) > 0){
                T tmp = array[i+1];
                array[i+1] =array[i];
                array[i] = tmp;
            }
        }
        return array[array.length-1];
    }
//    public void setPos(int pos, T array){
//        this.array[pos] = array;
//    }
}
class Alg3{
    //成员方法
    public <T extends Comparable> T findMaxVal(T[] array){
        for (int i = 0; i < array.length-1; i++) {
            if(array[i].compareTo(array[i+1]) > 0){
                T tmp = array[i+1];
                array[i+1] =array[i];
                array[i] = tmp;
            }
        }
        return array[array.length-1];
    }
}
class Alg2{
    //静态方法
    public static <T extends Comparable> T findMaxVal(T[] array){
        for (int i = 0; i < array.length-1; i++) {
            if(array[i].compareTo(array[i+1]) > 0){
                T tmp = array[i+1];
                array[i+1] =array[i];
                array[i] = tmp;
            }
        }
        return array[array.length-1];
    }
}
class MyArray<T>{

   // public T[] array = (T[]) new Object[10];
    public T[] array;
    public MyArray(){
    }

    public MyArray(Class<T> clas,int capacity){
        array = (T[]) Array.newInstance(clas,capacity);
    }

    public T getPos(int pos){
        return array[pos];
    }

    public void setPos(int pos, T array){
        this.array[pos] = array;
    }
    public T[] getMyarray(){
        return array;
    }
}
public class TestDemo1 {
    public static void main(String[] args) {
        Integer[] arr = {1,3,2,5,7};
        int val = Alg2.findMaxVal(arr);
        System.out.println(val);
    }
    public static void main3(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] arr = {1,3,2,5,7};
        int val = alg.findMaxVal(arr);
        System.out.println(val);
    }
    public static void main2(String[] args) {
        MyArray<Integer> myArray = new MyArray<>(Integer.class,10);
        Integer[] tmp = myArray.getMyarray();
        System.out.println(Arrays.toString(tmp));
    }
    public static void main1(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();
        myArray.setPos(0,10);
        myArray.setPos(1,20);
        System.out.println(myArray.getPos(1));

        MyArray<String> myArray1 = new MyArray<>();
        myArray1.setPos(0,"123");
        myArray1.setPos(1,"hll");
        System.out.println(myArray1.getPos(1));

    }
}
